// credits/components/prize-draw/index.js
Component({

    /**
     * 组件的属性列表
     */
    properties: {
      prize: {
        type: String,
        value: ""
      }
    },

    /**
     * 组件的初始数据
     */
    data: {
      isTurnOver: true,
      animationRotate: null,
      prizeAry: [0, 50, 80, 100, 180, 680, 880, 1000, 1680, 1980]
    },

    /**
     * 组件的方法列表
     */
    methods: {
      handleClose() {
        this.triggerEvent("close")
      },
      handlePrizeDraw() {
        if (this.data.isTurnOver) {
          this.setData({
            isTurnOver: false
          })

          let prizeId = Math.ceil(Math.random() * 10)
          console.log(this.data.prize)
          this.data.prizeAry.forEach((item, index) => {
            if (item == this.data.prize) {
              prizeId = index + 1;
            }
          })
          this.onRotate(prizeId)
        } else {
          wx.showToast({
            title: "请勿重复点击",
            duration: 2000,
            icon: "none"
          })
        }
      },
      onRotate(prizeId) {
        let _duration = 5000;
        let animationRotate = wx.createAnimation({
          duration: _duration,
          timingFunction: 'ease'
        })

        animationRotate.rotate(0).step({
          duration: 1
        })

        let angle = 3600 + 36 * prizeId - 18;
        animationRotate.rotate(angle).step()
        this.setData({
          animationRotate: animationRotate.export()
        })

        // let toastTitle = "恭喜您抽中了" + this.data.prizeAry[prizeId - 1] + "积分";
        // if (prizeId == 1) {
        //   toastTitle = "很遗憾，您抽中了0积分";
        // }
    
        setTimeout(() => {
          // wx.showToast({
          //   title: toastTitle,
          //   duration: 2000,
          //   icon: "none"
          // })
          this.setData({
            isTurnOver: true
          })
          this.triggerEvent("complete")
        }, _duration)
      }
    }
})